
/*
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
*/
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author Klaas Dons
 */
public class SetPlayer implements Runnable {
    private String playerName;
    
    public SetPlayer(String playerName){
        this.playerName = playerName;
    }
    
    /**
     * Zorgt ervoor dat de betreffende spelersnaam wordt opgeslagen op de mobiele telefoon.
     */
    public void run(){
        RecordStore rs = null;
        RecordEnumeration re = null;
        
        try {
            rs = RecordStore.openRecordStore("PlayerName", true);
            re = rs.enumerateRecords(null, null, false);
            
            byte[] raw = this.playerName.getBytes();
            
            if (re.hasNextElement()){
                int id = re.nextRecordId();
                rs.setRecord(id, raw, 0, raw.length);
            } else {
                rs.addRecord(raw, 0, raw.length);
            }
        } catch (RecordStoreException rse){
        } finally {
            if (re != null){
                re.destroy();
            }
            
            if (rs != null){
                try {
                    rs.closeRecordStore();
                } catch (RecordStoreException rse){
                }
            }
        }
        
        /*
        FileConnection fc = null;
        DataOutputStream dos = null;
        
        try {        
            fc = (FileConnection)Connector.open("file:///root1/player.txt", Connector.READ_WRITE);
            dos = new DataOutputStream(fc.openOutputStream());
            
            if (fc.exists()){
                fc.delete();
                fc = (FileConnection)Connector.open("file:///root1/player.txt", Connector.READ_WRITE);
            }
            
            fc.create();
            dos.write(this.playerName.getBytes());
            dos.flush();
        } catch (IOException e) {
        } finally {
            if (dos != null){
                try {
                    dos.close();
                } catch (IOException e) {
                }
            }
            
            if (fc != null){
                try {
                    fc.close();
                } catch (IOException e){
                }
            }
        }
        */
    }
}
